package ipad.asia.controller;

import ipad.asia.dao.EvaluateDao;
import ipad.asia.entity.Evaluate;
import ipad.asia.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class EvaluateController {
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private EvaluateDao evaluateDao;
    /**查看消费者顾客的评价*/
    @RequestMapping("/evaluate/getByConsumerName")
   public List<Evaluate> findByConsumerName(@RequestParam(value = "consumerName") String consumerName){
        return evaluateDao.findByConsumerName(consumerName);
    }
    /**查看商家的评价*/
    @RequestMapping("/evaluate/getByShopId")  //方便测试
   public List<Evaluate> findByShopId(@RequestParam(value = "shopId") int shopId){
       return evaluateService.findByShopId(shopId);
   }
    /**根基评价找人或商家或发评价的人*/
    @RequestMapping("/evaluate/getById")
   public Evaluate findById(@RequestParam(value = "id") int id){
       return evaluateService.findById(id);
   }
    /**发布评价*/
    @RequestMapping("/evaluate/launch")
   public void launch(@RequestParam(value = "consumerName") String consumerName,
                      @RequestParam(value = "shopId") int shopId,
                      @RequestParam(value = "productId") int productId,
                      @RequestParam(value = "orderId") int orderId,
                      @RequestParam(value = "content",required = false) String content,
                      @RequestParam(value = "level",required = false) Integer level,
                      @RequestParam(value = "date") LocalDateTime date){
        Evaluate evaluate=new Evaluate();
        evaluate.setOrderId(orderId).setConsumerName(consumerName).setShopId(shopId)
                .setProductId(productId).setContent(content).setLevel(level).setDate(date);
       evaluateDao.save(evaluate);
   }

    @RequestMapping("/evaluate/findByProductId")
    public List<Evaluate> findByProductId(@RequestParam(value = "productId")int productId){
       return evaluateDao.findByProductId(productId);
    }
    /**修改的评价,只能修改一次，修改后chance变为1,返回值为true表示可修改，返回值为发；false表示已经修改过改评论*/
//    @RequestMapping("/evaluate/update")
//   public boolean update(@RequestParam(value = "id") int id,
//                         @RequestParam(value = "content",required = false) String content,
//                         @RequestParam(value = "level",required = false) Integer level,
//                         @RequestParam(value = "date") LocalDateTime date,
//                         @RequestParam(value = "chance") Integer chance){
//      return evaluateService.update(id, content, level, date, chance);
//   }
    @RequestMapping("/evaluate/findByOrderId")
    public Evaluate findByOrderId(@RequestParam(value = "orderId",required = false) int orderId){
         return evaluateDao.findByOrderId(orderId);
    }
}
